package com.dailyblue.javaee.controller;

import com.alibaba.fastjson2.JSONArray;
import com.dailyblue.javaee.bean.Emp;
import com.dailyblue.javaee.service.EmpService;
import com.dailyblue.javaee.service.impl.EmpServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet ("/emp_find_ajax")
public class EmpFindAjaxController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. 产生service对象
        EmpService service = new EmpServiceImpl();
        // 2. 调用方法获取list
        List<Emp> list = service.find();
        // 3. 将list转换成json
        String json = JSONArray.toJSONString(list);
        // 4. 输出json
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(json);
    }
}
